from PIL import Image
import os

def image_cropper(path: str, file_name:str) -> None:
    """
    剪裁图像为 4:3 屏幕一半尺寸
    """
    img = Image.open(path)
    crop_img = img.crop((0,0,900,600))
    os.remove(path=path)
    crop_img.save(file_name)

def crop_all_images()->None:
    """
    将所有图像剪裁为 900 * 600 像素
    """
    index = 1
    path = './images/'
    files_list = os.listdir(path=path)
    images_name = list()
    for file in files_list:
        if '.jpeg' in file:
            images_name.append(file)
    images_name.sort(key=lambda x: int(x.split('.')[0]))
    for file in images_name:
        if '.jpeg' in file:
            image_cropper(path+file, './images/' + str(index)+'.jpeg')
            index += 1
    print('>>> Step.3 全部图像已剪裁为 900px * 600px.')
crop_all_images()